package net.zoneland.knowledge.utils;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.List;
import java.util.Optional;

/**
 * 同一单元格部分文字颜色、样式修改.
 * @author zx
 */
public class WriteHandlerStrategy implements CellWriteHandler {

    /**
     * key 搜索的关键词
     */
    private List<String> meetingNames;

    /**
     * 需要标红的列.
     */
    private final List<Integer> indexes;

    public WriteHandlerStrategy(final List<String> meetingNames, final List<Integer> indexes) {
        this.meetingNames = meetingNames;
        this.indexes = indexes;
    }

    /**
     * Called after all operations on the cell have been completed
     *
     * @param writeSheetHolder
     * @param writeTableHolder Nullable.It is null without using table writes.
     * @param cellDataList     Nullable.It is null in the case of add header.There may be several when fill the data.
     * @param cell
     * @param head             Nullable.It is null in the case of fill data and without head.
     * @param relativeRowIndex Nullable.It is null in the case of fill data.
     * @param isHead           It will always be false when fill data.
     */
    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (!isHead) {
            final boolean indexExist = indexes.stream().noneMatch(in -> cell.getColumnIndex() == in);
            if (indexExist) {
                return;
            }

            final String content = cell.getStringCellValue();
            if (StringUtils.isNotBlank(content)) {
                final String cont = StringUtils.replace(content, "||", ExcelUtils.CONTENT_SPLIT_SYMBOL);
                meetingNames.stream()
                    .filter(cont::contains)
                    .findFirst()
                    .ifPresent(meetingName -> {
                        final Sheet sheet = writeSheetHolder.getSheet();
                        final Workbook workbook = sheet.getWorkbook();
                        // xlsx格式，如果是老版本格式的话就用 HSSFRichTextString
                        final XSSFRichTextString richString = new XSSFRichTextString(cont);
                        final Font font = workbook.createFont();
                        font.setColor(Font.COLOR_RED);
                        font.setBold(true);
                        final int indexOf = cont.indexOf(meetingName);
                        // startIndex，endIndex 从哪到哪，你想设置成什么样的字体都行
                        richString.applyFont(indexOf, indexOf + meetingName.length(), font);
                        // 再设置回每个单元格里
                        cell.setCellValue(richString);
                    });
            }
        }
    }

}
